package app.netant.galgame.sadbrother;

import java.util.HashMap;
import java.util.Map;

import org.jsoup.select.Elements;

import app.client.file.downs.FileDown;
import app.netant.NetAntBase;
import cn.lily.com.file.FilePath;
import cn.lily.com.lang.number.MathUtil;
/**
 * 忧伤的弟弟页面处理类
 * @author JiaJ
 * @date 2018年6月12日
 *
 */
public class SadBrotherAntDetail extends NetAntBase {
		
	public SadBrotherAntDetail(String netSite, int timeout) {
		super(netSite, timeout);
	}
	
	public SadBrotherAntDetail(String netSite) {
		super(netSite);
	}
	
	private String type = "";
	private String title = "";
	private String memo = "";
	private String mark = "";
	private String staff = "";
	private Map<String,String> imageMap = new HashMap<String,String>();
	
	private void getType(){		
		Elements activePart = document.select("#zan-bodyer .article.container .breadcrumb a.taxonomy.category");
		if(activePart.size()>0){			
			this.type = activePart.get(0).text();
		}else{
			this.type = "";
		}	
	}

	private void getTitle(){		
		Elements activePart = document.select("#zan-bodyer .article.container .title-article a");
		if(activePart.size()>0){
			this.title = activePart.get(0).text();
		}else{
			this.title = "";
		}
		
		this.title = this.title.replaceAll("'", "’").replaceAll("\"", "”");
		
	}
	
	private void getImagePath(){		
		Elements activePart = document.select("#zan-bodyer .article.container .centent-article .dAnim > a");
		for(int i=0;i<activePart.size();i++){			
			String imgHref = activePart.get(i).attr("href");		
			String filePath = this.downImage(imgHref);			
			this.imageMap.put(imgHref, filePath);			
		}
	}
	
	private void getMemo(){
		
		Elements activePart = document.select("#zan-bodyer .article.container .centent-article .alert.alert-success");		
		if(activePart.size()>0){			
			this.memo = activePart.get(0).html().replaceAll("&nbsp;", "").replaceAll("<br>", "").replaceAll("<p>", "").replaceAll("</p>", "");

		}else{
			this.memo = "";
		}
		
		if(this.memo.length() > 1000){
			this.memo  = this.memo.substring(0,1000)+"...";
		}
		
		this.memo = this.memo.replaceAll("'", "’").replaceAll("\"", "”");
				
		if(activePart.size()>1){
			this.staff = activePart.get(0).html().replaceAll("&nbsp;", "").replaceAll("<br>", "").replaceAll("<p>", "").replaceAll("</p>", "");
		}else{
			this.staff = "";
		}
		
		if(this.staff.length() > 1000){
			this.staff  = this.staff.substring(0,1000)+"...";
		}
		
		this.staff = this.staff.replaceAll("'", "’").replaceAll("\"", "”");
	}
	
	private void getMark(){
				
		Elements activePart = document.select("#zan-bodyer .article.container .centent-article .panel.panel-primary .panel-footer");
		if(activePart.size()>0){
			
			this.mark = activePart.get(0).text();
			int first = this.mark.indexOf("备注");
			if(first>=0){
				this.mark = this.mark.substring(first);
			}else{
				this.mark = "";
			}
					
		}else{
			this.mark = "";
		}
		
		if(this.mark.length() > 1000){
			this.mark  = this.mark.substring(0,1000)+"...";
		}
		
		this.mark = this.mark.replaceAll("'", "’").replaceAll("\"", "”");
	}
	
	private String downImage(String imgHref){
		
		int endPoint = imgHref.lastIndexOf("/");		
		String fileName = imgHref.substring(endPoint+1);
		String fileExp = fileName.substring(fileName.lastIndexOf(".")+1);	
		String retPath = "/file/netant/sadbrother/"+System.currentTimeMillis()+""+MathUtil.random(3)+"."+fileExp;
		String filePath = FilePath.getWebRootPath(retPath);
		
		try {
			FileDown down = new FileDown();
			down.downImage(imgHref, filePath);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		
		return retPath;
		
	}
	
	private void dbAction(){
		
		//主表更新
		String mainID = System.currentTimeMillis()+""+MathUtil.random(3)+"";		
		String mainSQL = " insert into ${schema}.LEO_SAD_BROTHER_INFO (LEO_GAL_ID,LEO_GAL_NAM,LEO_GAL_MEMO,LEO_GAL_REMARK,LEO_GAL_STAFF,LEO_SITE_ANALYSE) "
				       + " values ('"+mainID+"','"+this.title+"','"+this.memo+"','"+this.mark+"','"+this.staff+"','"+this.netSite+"') ";
		this.getDBManager().executeUpdate(mainSQL);
		
		System.out.println("游戏数据更新成功！");
		
		//图片数据更新
		for(String imgPath:this.imageMap.keySet()){
			String filePath = this.imageMap.get(imgPath);
			String imageID = System.currentTimeMillis()+""+MathUtil.random(3)+"";
			String detailSQL = "insert into  ${schema}.LEO_SAD_BROTHER_IMG (LEO_IMG_ID,LEO_GAL_ID,LEO_IMG_PATH,LEO_IMG_FILE) "
					        + " values ('"+imageID+"','"+mainID+"','"+imgPath+"','"+filePath+"')";
			this.getDBManager().executeUpdate(detailSQL);
		}
		
		System.out.println("图片数据更新成功！");
		
	}
	
	public void analyse(){
		
		System.out.println(this.netSite+"：信息获取成功！");
		
		this.getType();
		
		if(!"汉化硬盘版".equals(this.type.trim())){
			return;
		}
		
		this.getTitle();
		this.getImagePath();
		this.getMemo();
		this.getMark();
		
		//插入数据库数据
		try {
			
			this.dbAction();
			
		} catch (Exception e) {
			e.printStackTrace();
			this.getDBManager().executeUpdate("insert into ${schema}.LEO_SAD_BROTHER_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
                    + "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+this.netSite+"','2') ");
		}
		
	}
	
	public static void main(String[] args) {
		String str = "11\"'22\"'33";
		System.out.println(str.replaceAll("\"", "'"));
	}
	
}
